package com.fingard.dsp.bank.reqrespobj.single.detail;

import java.util.HashMap;

import com.fingard.FGBiz;
import com.fingard.dsp.core.specialdeal.PayStateHelper;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingle;
import com.fingard.dsp.core.DSPSecurityData;
import com.fingard.luna.framework.boot.core.util.SpringContextHolder;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextWriter;

/**
 * 普通单笔支付、批量代收付的明细节点
 */
public class RetAtsSglDetail {
    public String respCode;
    public String respInfo;
    public String transState;
    public String reqSeqID;
    public String payInfoCode;
    public String transAmt;

    /**
     * 短信Token，用于短信扣款二次提交
     */
    public String token;

    /**
     * 渠道方的订单号，或者流水号
     */
    public String outTransID;

    /**
     * 是否需要占用资金池额度  (1-要占用额度，0-不占用额度)
     */
    public String isNeedLimit;

    /**
     * 失败类型	 (1-客户账户类错误,2-系统类错误,3-渠道商类错误,4-其它)
     */
    public String failType;

    public RetAtsSglDetail() {
    }

    /**
     * 增加单笔代收付提交返回响应类重载构造方法
     *
     * @param p_req
     */
    public RetAtsSglDetail(ReqSingle p_req) {
        reqSeqID = p_req.singleDetail.askNumber;
    }

    /**
     * 增加单笔代收付查询返回响应类重载构造方法
     *
     * @param p_req
     */
    public RetAtsSglDetail(ReqQrySingle p_req) {
        reqSeqID = p_req.askNumber;
    }

    public void writeToXml(XmlTextWriter retXmlWriter, String p_bankCode, String p_transType) throws Exception {
        retXmlWriter.writeStartElement("DetailRecord");
        if ("0".equals(respCode) && !DSPSecurityData.getInstance().isReadFileFlag()) {
            PayStateHelper payStateHelper = SpringContextHolder.getBean(PayStateHelper.class);
            PayStateHelper stateHelper = payStateHelper.convertTransResult(p_bankCode, p_transType, transState, payInfoCode);
            transState = stateHelper.payState;
            payInfoCode = stateHelper.payInfo;
        }

        if (FGBiz.dspCenter.convertPayState && respCode.equals("0")) {
            PayStateHelper payStateHelper = SpringContextHolder.getBean(PayStateHelper.class);
            payStateHelper.setBankCode(p_bankCode);
            payStateHelper.setTransType(p_transType);
            payStateHelper.setRespInfo(respInfo);
            payStateHelper.setBankTransState(transState);
            payStateHelper.setBankPayInfoCode(payInfoCode);
            payStateHelper.toBank();
            transState = payStateHelper.payState;
            payInfoCode = payStateHelper.payInfo;
        }

        retXmlWriter.writeElementString("RespCode", respCode);
        retXmlWriter.writeElementString("RespInfo", respInfo);
        retXmlWriter.writeElementString("TransState", transState);
        retXmlWriter.writeElementString("ReqSeqID", reqSeqID);
        retXmlWriter.writeElementString("PayInfoCode", payInfoCode);
        retXmlWriter.writeElementString("TransAmt", transAmt);
        retXmlWriter.writeElementString("OutTransID", outTransID);
        if (StringHelper.hasAnyChar(token)) {
            retXmlWriter.writeElementString("Token", token);
        }
        retXmlWriter.writeElementString("NeedLimit", isNeedLimit);
        retXmlWriter.writeElementString("FailType", failType);
        retXmlWriter.writeEndElement();
    }

    public HashMap<String, String> writeToHash() {
        HashMap<String, String> retHash = new HashMap<String, String>();

        retHash.put("RespCode", respCode);
        retHash.put("RespInfo", respInfo);
        retHash.put("RdSeq", reqSeqID);
        if (StringHelper.hasAnyChar(transState)) {
            retHash.put("TransState", transState);
        }
        if (StringHelper.hasAnyChar(payInfoCode)) {
            retHash.put("PayInfoCode", payInfoCode);
        }
        if (StringHelper.hasAnyChar(outTransID)) {
            retHash.put("OutTransID", outTransID);
        }
        if (StringHelper.hasAnyChar(isNeedLimit)) {
            retHash.put("NeedLimit", isNeedLimit);
        }
        if (StringHelper.hasAnyChar(failType)) {
            retHash.put("FailType", failType);
        }

        return retHash;
    }
}
